<?php

class kyoCurl
{
    private $ch = null;
    private $info = null;
    static private $instrance = null;

    public function __construct()
    {
        $this->ch = curl_init();
        $this->init();
    }

    public function __destruct()
    {
        curl_close($this->ch);
    }

    static public function getInstrance()
    {
        if (self::$instrance === null)
            self::$instrance = new kyoCurl();

        return self::$instrance;
    }

    public function init()
    {
        curl_reset($this->ch);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }

    public function get($url, $header = null)
    {
        return $this->send($url, null, 'GET', $header);
    }

    public function post($url, $data = null, $header = null)
    {
        return $this->send($url, $data, 'POST', $header);
    }

    public function put($url, $data = null, $header = null)
    {
        return $this->send($url, $data, 'PUT', $header);
    }

    public function del($url, $header = null)
    {
        return $this->send($url, null, 'DELETE', $header);
    }

    public function send($url, $data = null, $method = 'GET', $header = null)
    {
        $this->init();
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
        if ($data !== null)
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
        if ($header !== null)
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);

        $s = curl_exec($this->ch);

        $this->info = curl_getinfo($this->ch);

        if ($this->info['http_code'] >= 200
                && $this->info['http_code'] <= 299)
            return $s;

        return false;
    }

    public function error()
    {
        return curl_error($this->ch);
    }

    public function getInfo()
    {
        return $this->info;
    }
}
